package vuelta.general.adapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import vuelta.general.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class VideoAdapter extends BaseAdapter {

	private JSONArray mVideoList;
	private Context mContext;

	public VideoAdapter(Context context, JSONArray stagesArray) {
		mContext = context;
		mVideoList = stagesArray;
	}

	public int getCount() {
		if (mVideoList != null) {
			return mVideoList.length();
		}
		return 0;
	}

	public Object getItem(int position) {
		if (mVideoList != null) {
			try {
				return mVideoList.get(position);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		LinearLayout view = (LinearLayout) convertView;

		if (convertView == null) {
			view = (LinearLayout) ((LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
					.inflate(R.layout.videos_item, null);
		}

		TextView videoNumber = (TextView) view.findViewById(R.id.video_name);
		videoNumber.setTextColor(mContext.getResources().getColor(R.color.blue));

		JSONObject o = null;
		try {
			o = (JSONObject) mVideoList.get(position);
		} catch (JSONException e1) {
			e1.printStackTrace();
		}

		try {

			videoNumber.setText(o.getString("descrizione"));

		} catch (JSONException e) {
			e.printStackTrace();
		}

		return view;
	}
}
